package com.qfedu.controller;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qfedu.bean.AtkSkill;
import com.qfedu.bean.PageVo;
import com.qfedu.bean.result.QueryResult;
import com.qfedu.bean.result.Result;
import com.qfedu.bean.result.ResultEnum;
import com.qfedu.service.IAtkSkillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * @Author WHW
 * @Description
 * @Date 2022/6/15
 */
@RestController//相当于 controller + responseBody
@RequestMapping("/atkSkill")
public class AtkSkillController {

    @Autowired
    IAtkSkillService atkSkillService;

    //查询分页信息
    @RequestMapping("/selectByPage")
    public  String selectByPage(PageVo vo) throws JsonProcessingException {
        //1.查询数据
        QueryResult<AtkSkill> qr = atkSkillService.selectByPage(vo);

        ObjectMapper objectMapper = new ObjectMapper();
        String s = objectMapper.writeValueAsString(qr);

        return  s;

    }

    @RequestMapping("/add")
    public String add(AtkSkill atkSkill) {

        Result result = null;
        int rs =  atkSkillService.add(atkSkill);

        if(rs==1) {
            result = new Result(ResultEnum.ADD_SUCCESS);
        }else {
            result = new Result(ResultEnum.ADD_ERROR);
        }

        return JSON.toJSONString(result);

    }

    @RequestMapping("/deleteById/{id}")
    public  String deleteById(@PathVariable("id") Integer id) {

        Result result ;

        int rs =  atkSkillService.deleteById(id);

        if(rs==1) {
            result =  new Result(ResultEnum.DELETE_SUCCESS);
        }else {
            result =  new Result(ResultEnum.DELETE_ERROR);
        }

        return  JSON.toJSONString(result);

    }

    @RequestMapping("/updateById")
    public String updateById(AtkSkill atkSkill) {
        Result result ;

        int rs =  atkSkillService.updateById(atkSkill);

        if(rs==1) {
            result =  new Result(ResultEnum.UPDATE_SUCCESS);
        }else {
            result =  new Result(ResultEnum.UPDATE_ERROR);
        }

        return  JSON.toJSONString(result);


    }

    @RequestMapping("/deleteBatch")
    public  String deleteBatch(Integer[] ids) {


        for (Integer id : ids) {
            atkSkillService.deleteById(id);
        }

        return  JSON.toJSONString(new Result(ResultEnum.DELETE_SUCCESS));



    }

}
